package controller

import (
	"hytx_manager/models"
	"hytx_manager/pkg/setting"
	"strconv"

	"github.com/astaxie/beego/validation"

	"github.com/Unknwon/com"
	"github.com/gin-gonic/gin"
)

func GetResers(c *gin.Context) {
	status := c.DefaultQuery("status", "")
	code := c.DefaultQuery("code", "")
	comName := c.DefaultQuery("com_name", "")

	page := com.StrTo(c.DefaultQuery("page", "1")).MustInt()
	limit := com.StrTo(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize))).MustInt()

	data, paginate := models.GetResers(page, limit, status, code, comName)

	render(c, gin.H{
		"paginate": paginate,
		"data":     data,
	})
}

func UseReserCode(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("ID必须大于0")
	if valid.HasErrors() {
		fail(c, "参数格式不对")
		return
	}
	if !models.ExistReserByID(id) {
		fail(c, "数据不存在")
		return
	}
	data := make(map[string]interface{})
	data["status"] = 1
	if !models.EditReserByID(id, data) {
		fail(c, "数据更改失败")
		return
	}
	success(c)
}
